/******************************************************************************

    Copyright (C) 2010 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.engine.syspreds;

import java.util.List;

import prettyprolog.datatypes.Cons;
import prettyprolog.datatypes.Constant;
import prettyprolog.datatypes.Nil;
import prettyprolog.datatypes.Substitution;
import prettyprolog.datatypes.Term;
import prettyprolog.engine.Engine;
import prettyprolog.engine.Function;
import prettyprolog.engine.FunctionException;

/**
 * Implements the apply/3 system predicate, that performs function application.
 * @author Alessio Stalla
 *
 */
public class Apply extends SystemPredicate {

	private static final Apply inst = new Apply();

	private Apply() {
		super(3);
	}

	/** Returns the only instance of this system predicate. */
	public static Apply getInstance() {
		return inst;
	}

	protected SyspredEffect doCall(Engine en, Substitution subst, List<Term> args) throws SyspredException {
		Term fname = args.get(0);
		Function function;
		Term retVal = args.get(2);
		try {
			function = en.getTheory().getFunction(((Constant) fname).getSymbol());
		} catch(ClassCastException e) {
			throw new SyspredException("Constant expected: " + fname, e);
		}
		if(function == null) {
			throw new SyspredException("Not a function: " + fname);
		}
		
		Term[] fargs;
		if(args.get(1) instanceof Cons) {
			Cons fargsList = (Cons) args.get(1);
			fargs = new Term[fargsList.length()];
			int i = 0;
			for(Term t : fargsList) {
				fargs[i++] = t;
			}
		} else if(args.get(1) == Nil.NIL) {
			fargs = new Term[0];
		} else {
			throw new SyspredException("List expected: " + args.get(1));
		}
		try {
			Term result = function.apply(fargs);
			Substitution s2 = en.getUnificationAlgorithm().unify(retVal, result);
			if(s2 != null) {
				return new SyspredEffect(false, subst.compose(s2));
			} else {
				return new SyspredEffect(true, subst);
			}
		} catch(FunctionException e) {
			throw new SyspredException("Error calling function " + fname, e);
		}
	}
	
	public boolean isBacktrackable() {
		return false;
	}

	public String toString() {
		return super.toString() + " (apply/3)";
	}
}